package thread.pc;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.util.Random;

import thread.invariantframework.Timeout;

public class PipedIO {

	public static void main(String[] args) throws IOException{
		Sender sender = new Sender();
		Receiver receiver = new Receiver(sender);
		sender.start();
		receiver.start();
		new Timeout(4000, "Terminated");
	}
}

class Sender extends Thread {
	private Random rand = new Random();

	private PipedWriter out = new PipedWriter();

	public PipedWriter getPipedWriter() {
		return out;
	}

	public void run() {
		while (true) {
			for (char c = 'A'; c <= 'z'; c++) {
				try {
					out.write(c);
					sleep(rand.nextInt(100));
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
		}
	}
}

class Receiver extends Thread {
	private PipedReader in;

	public Receiver(Sender sender) throws IOException {
		in = new PipedReader(sender.getPipedWriter());
	}

	@Override
	public void run() {
		try {
			while (true) {
				// Block untl characters are there:
				System.out.println("Reader: " + (char) in.read());
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}